<template>
  <div class="container">
    <div class="search">
      <van-icon name="arrow-left" @click="gotoBack" />
      <van-search v-model="value" placeholder="请输入搜索关键词" class="search-input" />
      <span class="search-btn" @click="gotoSearchList(value)">搜索</span>
    </div>

    <div class="history">
      <p class="title"><van-icon name="search" /> 历史搜索</p>

      <div class="history-list">暂无历史搜索</div>
    </div>

    <div class="hot">
      <p class="title"><van-icon name="star-o" /> 热门搜索</p>

      <van-button
        class="hot-btn"
        type="default"
        hairline
        plain
        size="small"
        :style="{ color: word.color }"
        v-for="(word, index) in words"
        :key="index"
        @click="gotoSearchList(word.frontword)"
      >
        {{ word.frontword }}
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { getHotSearch } from '@/api/index.js'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

const words = ref([])
const value = ref('')

onMounted(async () => {
  const res = await getHotSearch()
  // console.log("search", res);
  words.value = res.data.hotWord.data
})

// 进入搜索列表页
const gotoSearchList = (keyword) => {
  router.push({ path: '/searchList', query: { q: keyword } })
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background-color: #fff;

  .search {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    // height: 44px;
    background-color: #fff;
    overflow: hidden;
    padding: 0 10px;
    border-top: 1px solid #eee;
    flex-wrap: nowrap;
    white-space: nowrap;

    .search-input {
      width: 100%;
    }

    .search-btn {
      flex: 1;
      margin-left: auto;
      flex-shrink: 0;
    }
  }

  .history {
    background-color: #fff;
    border-bottom: 1px solid #eee;
    padding: 10px;

    .title {
      color: #999;
      font-size: 13px;
      margin: 0;
      padding: 0;
    }

    .history-list {
      font-size: 13px;
      color: #999;
      margin-top: 10px;
      margin-left: 18px;
    }
  }

  .hot {
    background-color: #fff;
    padding: 10px;

    .title {
      color: #999;
      font-size: 13px;
      margin: 0;
      padding: 0;
    }

    .hot-btn {
      margin: 10px auto 0 10px;
    }
  }
}
</style>
